<template>
  <div>
    <el-button type="primary" @click="handleCheck" class="mb8">新增</el-button>
    <el-table :data="tableList" style="height: calc(100vh - 250px)">
      <el-table-column prop="id" label="ID" width="150" />
      <el-table-column label="物流名称">
        <template #default="scope">
          <el-space>
            <el-tooltip effect="dark" :content="scope.row.name">
              <el-text truncated style="width: 100px;">{{
                scope.row.name
              }}</el-text>
            </el-tooltip>
            <el-button link type="primary" size="small" @click="viewTranslate(scope.row)">查看翻译</el-button>
          </el-space>
        </template>
      </el-table-column>
      <el-table-column prop="threshold" label="参考阈值" align="center" />
      <el-table-column prop="amount" label="参考价格" align="center" />
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-text="开启"
            inactive-text="关闭" inline-prompt @change="changeStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="newLastModified" label="修改时间" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="handleEditor(scope.row)">编辑</el-button>
          <!-- <el-button  link type="danger" @click="handleDelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <Pagination :total="total" :limit="searchFrom.pageSize" :page="searchFrom.pageNum" @pagination="handlePageChange">
    </Pagination>
    <!-- 查看翻译 -->
    <Translation :dialogVisible="openViewTranslate" :languageList="translateData"
      @handleTranslationClose="handleTranslationClose"></Translation>
    <RecursiveIntegration :item="publicItem" :openRecursive="openRecursive" @handleSure="handleSure"
      @handleClose="handleClose"></RecursiveIntegration>
  </div>
</template>
<script setup>
import { reactive, ref, onMounted } from "vue";
import RecursiveIntegration from "./components/recursiveIntegration.vue";
const { proxy } = getCurrentInstance();
const openViewTranslate = ref(false);
const translateData = ref([]); //查看翻译数据
const publicItem = ref({}); //公共赋值对象
const openRecursive = ref(false);
const total = ref(10);
const tableList = ref([]);
const searchFrom = ref({
  pageNum: 1,
  pageSize: 20,
  param: {},
});
onMounted(() => {
  getList();
});
const getList = () => {
  site.general
    .sitetransportList({
      data: searchFrom.value,
    })
    .then((res) => {
      total.value = res.total;
      if (!res.data?.length) return;
      res.data = res.data.map((code) => {
        return {
          ...code,
          newLastModified: proxy.DateTime(code.lastModified),
        };
      });
      tableList.value = res.data;
      console.log(res, "快递类别");
    });
};
const handleEditor = (rowItem) => {
  publicItem.value = rowItem;
  openRecursive.value = true;
};
// 删除
const handleDelete = (rowItem) => {
  let newIds = [];
  newIds.push(rowItem.id);
  // site.general.sitetransportDeletebatch({

  proxy.$modal.confirm("是否确认删除此数据？").then(() => {
    return site.general
      .sitetransportDeletebatch({
        data: {
          ids: newIds,
        },
      })
      .then((res) => {
        if (res.code !== 200) return;
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
  });
};
// 改变状态
const changeStatus = (e) => {
  proxy.$modal
    .confirm("确定需要修改此状态吗？")
    .then(() => {
      site.general
        .sitetransportUpdatestatus({
          pathParams: {
            id: e.id,
          },
          params: {
            status: e.status,
          },
        })
        .then((res) => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess("修改状态成功");
            getList();
          }
        });
    })
    .catch((err) => {
      if (e.status) {
        return (e.status = 0);
      } else {
        return (e.status = 1);
      }
    });
};

// 分页
const handlePageChange = (newPage) => {
  searchFrom.value.pageNum = newPage.page;
  searchFrom.value.pageSize = newPage.limit;
  getList();
};
// 查看翻译多语言
const viewTranslate = (rowItem) => {
  site.general
    .sitetransportGetbyid({
      pathParams: {
        id: rowItem.id,
      },
    })
    .then((res) => {
      openViewTranslate.value = true;
      translateData.value = res.data.descriptions;
    });
};
// 新增
const handleCheck = () => {
  publicItem.value = {};
  openRecursive.value = true;
};
// 取消
// 确定
const handleClose = () => {
  openRecursive.value = false;
};

const handleSure = () => {
  getList();
  openRecursive.value = false;
};
const handleTranslationClose = () => {
  openViewTranslate.value = false;
};
</script>